package com.hns.can.common.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import com.hns.can.common.constants.GlobalConst;

@Component("AppInitManager")
public class AppInitManager implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private CacheTaskManager cacheTaskManager;
    
    @Autowired
    private SysCacheService sysCacheService;
    
    /**
     * @param arg0
     * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if(event.getApplicationContext().getParent() == null){
            cacheTaskManager.addTask(GlobalConst.CACHE_KEY_SYS_CODE, new Task() {
                @Override
                public Object run(String corpId) {
                    return sysCacheService.setSysCodeCache(corpId);
                }
            });
            cacheTaskManager.addTask(GlobalConst.CACHE_KEY_SYS_ROLE, new Task() {
                @Override
                public Object run(String corpId) {
                    return sysCacheService.setSysRoleCache(corpId);
                }
            });
            
            cacheTaskManager.addTask(GlobalConst.CACHE_KEY_SYS_ACCOUNT, new Task() {
                @Override
                public Object run(String corpId) {
                    return sysCacheService.setSysAccountCache(corpId);
                }
            });
            
            cacheTaskManager.addTask(GlobalConst.CACHE_KEY_BASE_ORGAN, new Task() {
                @Override
                public Object run(String corpId) {
                    return sysCacheService.setBaseOrganCache(corpId);
                }
            });
            
            cacheTaskManager.addTask(GlobalConst.CACHE_KEY_SYS_CORP, new Task() {
                @Override
                public Object run(String corpId) {
                    return sysCacheService.setSysCorpCache();
                }
            });
            
            cacheTaskManager.addTask(GlobalConst.CACHE_KEY_BASE_CAR_INFO, new Task() {
                @Override
                public Object run(String corpId) {
                    return sysCacheService.setBaseCarInfoCache(corpId);
                }
            });
            
            cacheTaskManager.addTask(GlobalConst.CACHE_KEY_BASE_LINE_INFO, new Task() {
                @Override
                public Object run(String corpId) {
                    return sysCacheService.setBaseLineInfoCache(corpId);
                }
            });
            
            cacheTaskManager.addTask(GlobalConst.CACHE_KEY_BASE_DRV_INFO, new Task() {
                @Override
                public Object run(String corpId) {
                    return sysCacheService.setBaseDrvInfoCache(corpId);
                }
            });
            cacheTaskManager.addTask(GlobalConst.CACHE_KEY_UPG_EQMT_VRSN_MANAGE_INFO, new Task() {
                @Override
                public Object run(String corpId) {
                    return sysCacheService.setUpgEqmtVrsnManageInfoCache(corpId);
                }
            });
            
            cacheTaskManager.addTask(GlobalConst.CACHE_KEY_BASE_EQMT_INFO, new Task() {
                @Override
                public Object run(String corpId) {
                    return sysCacheService.setBaseEqmtInfoCache(corpId);
                }
            });
            
            cacheTaskManager.addTask(GlobalConst.CACHE_KEY_BASE_STN_INFO, new Task() {
                @Override
                public Object run(String corpId) {
                    return sysCacheService.setBaseStnInfoCache(corpId);
                }
            });
            //======================================================
            
            cacheTaskManager.addTask(GlobalConst.CACHE_ALARM_TIME_SETTING, new Task() {
                @Override
                public Object run(String corpId) {
                    return sysCacheService.setAlarmTimeSetCache(corpId);
                }
            });
            
            cacheTaskManager.addTask(GlobalConst.CACHE_ALARM_FAULT_SETTING, new Task() {
                @Override
                public Object run(String corpId) {
                    return sysCacheService.setAlarmFaultSetCache(corpId);
                }
            });
            
            cacheTaskManager.addTask(GlobalConst.CACHE_ALARM_UNSAFE_SETTING, new Task() {
                @Override
                public Object run(String corpId) {
                    return sysCacheService.setAlarmUnsafeSetCache(corpId);
                }
            });
            
            cacheTaskManager.addTask(GlobalConst.CACHE_ALARM_SPEED_SETTING, new Task() {
                @Override
                public Object run(String corpId) {
                    return sysCacheService.setAlarmSpeedSetCache(corpId);
                }
            });
        }
    }
	
}
